#ifndef _N2S_H
#define _N2S_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

#define BIG_LITTLE_ENDIAN_TRAN_U16(p) ((*(p) << 8) + (*((p) + 1)))
#define BIG_LITTLE_ENDIAN_TRAN_U32(p) ((*(p) << 24) + ((*((p) + 1)) << 16) + ((*((p) + 2)) << 8) + (*((p) + 3)))

// n2s
#define N2S_STRUCT_GET_uint8_t_ELEMENT(to_struct, from_netbuf, _element)                                               \
    (to_struct)->_element = *(uint8_t *)from_netbuf;                                                                   \
    from_netbuf += sizeof(char);

#define N2S_STRUCT_GET_uint16_t_ELEMENT(to_struct, from_netbuf, _element)                                              \
    (to_struct)->_element = BIG_LITTLE_ENDIAN_TRAN_U16((uint8_t *)from_netbuf);                                        \
    from_netbuf += sizeof(int16_t);

#define N2S_STRUCT_GET_uint32_t_ELEMENT(to_struct, from_netbuf, _element)                                              \
    (to_struct)->_element = BIG_LITTLE_ENDIAN_TRAN_U32((uint8_t *)from_netbuf);                                        \
    from_netbuf += sizeof(int32_t);

#define N2S_STRUCT_GET_string_ELEMENT(to_struct, from_netbuf, _element)                                                \
    memmove((to_struct)->_element, from_netbuf, sizeof((to_struct)->_element));                                        \
    from_netbuf += sizeof((to_struct)->_element);

#define N2S_STRUCT_GET_BASIC_ELEMENT(to_struct, from_netbuf, type, _element)                                           \
    N2S_STRUCT_GET_##type##_ELEMENT(to_struct, from_netbuf, _element)

// function
/* netbuf to structure object */
#define n2s_struct_get_basic_element(to_struct, from_netbuf, type, element)                                            \
    N2S_STRUCT_GET_BASIC_ELEMENT(to_struct, from_netbuf, type, element)

#ifdef __cplusplus
}
#endif

#endif
